/*
 * @Date: 2021-08-18 09:40:03
 * @information:
 */
import Sequelize from 'sequelize'
import sequelizeMysql from '../utils/sequelizeMysql'
import moment from 'moment'


// 创建 model
const LikeDiary = sequelizeMysql.define(
  'like_diary',
  {
    id: {
      type: Sequelize.UUID,
      defaultValue: Sequelize.UUIDV1,
      primaryKey: true,
    },
    diaryId: {
      type: Sequelize.STRING(255),
      allowNull: false, // allowNull不设置默认为true
      field: 'diary_id',
    },
    toUserId: {
      type: Sequelize.STRING(255),
      allowNull: false,
      field: 'to_user_id',
    },
    fromUserId: {
      type: Sequelize.STRING(255),
      allowNull: false,
      field: 'from_user_id',
    },
    createtime: {
      type: Sequelize.STRING(255),
      defaultValue: Date.now(),
      get () {
        const value = this.getDataValue('createtime')
        return value ? moment(Number(value)).format('YYYY-MM-DD HH:mm:ss') : value
      }
    },
    updatetime: {
      type: Sequelize.STRING(255),
      get () {
        const value = this.getDataValue('updatetime')
        return value ? moment(Number(value)).format('YYYY-MM-DD HH:mm:ss') : value
      }
    },
    isdelete: {
      type: Sequelize.INTEGER,
      defaultValue: 0,
      allowNull: false,
    }
  },
  {
    // true 表名称和 model 相同: diary
    // false 创建表名称会是复数: diarys
    freezeTableName: true,
    // 是否使用默认的 createdAt updatedAt 
    timestamps: false,
  },
)

// 创建表
LikeDiary.sync({ force: false })

export default LikeDiary
